<?php

/**
 * @Author: wuchenge
 * @Date: 2020-12-07 15:59:42
 * @email: ligui0506@sina.com
 * @descript: 管理员控制器
 */

namespace App\Admin\v1\Rbac;

use App\Http\Controllers\ApiController;
use App\Libraries\Utils;
use App\Models\Admin;
use App\Models\Rbac\Role;
use App\Requests\rbac\AdminRequest;
use App\Services\Permission;
use Illuminate\Http\Request;

class AdminController extends ApiController
{
    public function lists(Request $request)
    {
        $field = ['id', 'account', 'pwd', 'nick_name', 'type', 'status'];
        $limit = $request->input('size', config('api.commmon.size'));
        $page  = $request->input('page', config('api.commmon.page'));
        // 节点标题
        $nick_name = $request->input('nick_name', '');
        $account   = $request->input('account', '');

        $where = [
            'nomal' => [],
        ];

        if (!empty($nick_name)) {
            array_push($where['nomal'], [
                'nick_name', 'like', $nick_name . '%',
            ]);
        }

        if (!empty($account)) {
            array_push($where['nomal'], [
                'account', 'like', $account . '%',
            ]);
        }

        $order = [];
        $with  = ['roles'];

        $result = Admin::lists($field, $where, $page, $limit, $order, $with);

        // 判断是否超级管理员
        foreach ($result['list'] as $key => $value) {
            $value['is_supper']   = Permission::isSupper($value['id']);
            $result['list'][$key] = $value;
        }

        return $this->response($result);
    }

    public function modify(AdminRequest $request)
    {
        $account   = $request->input('account', '');
        $nick_name = $request->input('nick_name', '');
        $status    = $request->input('status', Admin::STATUS_YES);
        $password  = $request->input('password', '');
        $id        = $request->input('id', 0);
        $admin_id  = $request->get('admin_id');
        $roles     = $request->input('roles', []);
        // 加密码
        $pwd = '';
        if (!empty($password)) {
            $pwd = Admin::encodePwd(md5($password));
        }
        $data = [
            'account'   => $account,
            'nick_name' => $nick_name,
            'status'    => $status,
            'roles'     => $roles,
            'admin_id'  => $admin_id,
            'id'        => $id,
        ];

        if (!empty($pwd)) {
            $data['pwd'] = $pwd;
        }

        if (empty($id)) {
            // 新增
            $res = Admin::store($data);
        } else {
            // 修改
            $res = Admin::modify($data);
        }

        if ($res) {
            return $this->response();
        }

        return $this->failed();
    }

    public function delete(Request $request)
    {
        $id  = $request->input('id');
        $ids = explode(',', $id);

        // 去掉超级管理员
        $supper = config('api.supper');
        foreach ($ids as $k => $id) {
            if (in_array($id, $supper)) {
                unset($ids[$k]);
            }
        }
        $res = Admin::del($ids);
        if ($res) {
            return $this->response();
        }
        return $this->failed();
    }

    public function center(AdminRequest $request)
    {
        $admin_id  = $request->get('admin_id');
        $nick_name = $request->input('nick_name', '');
        $password  = $request->input('password', '');
        // 加密码
        $pwd = '';
        if (!empty($password)) {
            $pwd = Admin::encodePwd(md5($password));
        }
        $data = [
            'nick_name' => $nick_name,
        ];

        if (!empty($pwd)) {
            $data['pwd'] = $pwd;
        }

        $admin = Admin::find($admin_id);
        if (!$admin) {
            return $this->failed();
        }
        $res = $admin->fill($data)->save();
        if ($res) {
            return $this->response();
        }
        return $this->failed();
    }

    public function options()
    {
        $field  = ['id', 'name'];
        $result = Role::lists($field, []);

        return $this->response($result);
    }
}
